package com.maotouying.recycleviewtest.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.maotouying.recycleviewtest.R;
import com.maotouying.recycleviewtest.beans.MoreTypeBean;

import java.util.List;

public class MoreTypeAdapter extends RecyclerView.Adapter {

    // 定义三个常量，标识三种类型
    public static final int TYPE_FULL_IMAGE = 0;
    public static final int TYPE_LEFT_IMAGE = 1;
    public static final int TYPE_RIGHT_IMAGE = 2;


    private final List<MoreTypeBean> mData;

    // 构造器，用来给mData赋值，调用此构造器，需要传入参数
    public MoreTypeAdapter(List<MoreTypeBean> data) {
        this.mData = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        /**
         * 根据viewType来创建条目  这样，条目就可以不一样了
         */
        View view;
        // 创建viewHolder
        if (viewType == TYPE_FULL_IMAGE) {
            view = View.inflate(parent.getContext(), R.layout.item_type_full_image, null);
            return new FullImageHolder(view);
        } else if (viewType == TYPE_LEFT_IMAGE) {
            view = View.inflate(parent.getContext(), R.layout.item_type_left_title_right_image, null);
            return new RightImageHolder(view);
        } else {
            view = View.inflate(parent.getContext(), R.layout.item_type_three_image, null);
            return new ThreeImagesHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    // 重写一个方法，这个方法根据条件返回条目类型
    @Override
    public int getItemViewType(int position) {
        MoreTypeBean moreTypeBean = mData.get(position);
        if (moreTypeBean.type == 0) {
            return TYPE_FULL_IMAGE;
        } else if (moreTypeBean.type == 1) {
            return TYPE_LEFT_IMAGE;
        } else {
            return TYPE_RIGHT_IMAGE;
        }
    }

    public class FullImageHolder extends RecyclerView.ViewHolder {
        public FullImageHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public class ThreeImagesHolder extends RecyclerView.ViewHolder {
        public ThreeImagesHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public class RightImageHolder extends RecyclerView.ViewHolder {
        public RightImageHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

}
